const express = require('express')
const path = require('path')
const app = express()
app.get('/', function(req, res){
  res.sendFile(path.resolve(__dirname, 'socket.html'))
})
app.listen(3000)

// ------------------------分割线----------------------

let WSServer = require('ws').Server
let wsServer = new WSServer({port: 8888})

// 监听客户端连接，
// socket每个客户端连接服务器，都会创建唯一的socket
wsServer.on('connection', function(socket){
  console.log('客户端已经连接')
  // 监听客户端消息
  socket.on('message', function(message){
    console.log(message)
    socket.send('server:' + message)
  })
})
